package demo518;


import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class TestList {
//1
    public void addEmp(Set<Emp> list, Emp emp) {

        list.add(emp);
    }
//2
     public void findAllEmp(Set<Emp> list) {
      for (Emp emp:list) {
          System.out.println(emp);
      }
    }


//3
    public Emp findEmpName(Set<Emp> list, String empName) {
        for (Emp emp:list) {
            if (empName.equals(emp.getName())) {
                return emp;
            }
        }
        return null;
    }
//4
    public void deleteEmp(Set<Emp> list, String empNumber) {
    Iterator<Emp> iterator = list.iterator();
    while (iterator.hasNext()) {
        Emp emp = iterator.next();
        if (empNumber.equals(emp.getNumber())) { // 比较的是Number而非Name
            iterator.remove();
        }
    }
}

//5
    public Emp findEmpNumber(Set<Emp> list, String empNumber) {
        for (Emp emp:list) {
            if (empNumber.equals(emp.getNumber())) {
                return emp;
            }
        }
        return null;
    }
//6
public Emp ListEmp(Set<Emp> empSet, Emp emp) {

    Iterator<Emp> iterator = empSet.iterator();
    while (iterator.hasNext()) {
        Emp existingEmp = iterator.next();
        if (emp.getNumber().equals(existingEmp.getNumber())) {
            iterator.remove();
            break;
        }
    }
    empSet.add(emp);
    return emp;
}
}

